package com.syezon.wifikey.splash;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.syezon.wifikey.splash.config.FrontAdConfig;
import com.syezon.wifikey.utils.Utils;

import java.util.HashMap;

public class FrontADService {

    private static String TAG = FrontADService.class.getSimpleName();

    public static final long ADVERTISEMENT_INTERNAL_TIME = 30 * 1000;//30s
    private static volatile FrontADService instance;
    private String PKG_NAME = Utils.getApp().getPackageName();

    private int mFinalCount;
    private HashMap<String, String> advFilter = new HashMap<>();
    private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {
            try {
                mFinalCount++;
                Log.e(TAG, "onActivityStarted: " + activity.getClass().getSimpleName() + ">>>" + mFinalCount);
                boolean canShowAd = FrontAdConfig.canShowAd();
                if (mFinalCount == 1
                        && canShowAd
                        && canShowAdvertisement(activity.getClass())
                        && NetworkUtils.isConnected()) {
                    //说明从后台回到了前台
                    long lastShowAdvertisementTimeStamp = FrontAdConfig.getLastFrontAdShowTime();
                    long curTime = System.currentTimeMillis();
                    if ((curTime - lastShowAdvertisementTimeStamp > ADVERTISEMENT_INTERNAL_TIME)) {
                        FrontAdConfig.saveLastFrontAdShowTime(curTime);
                        ActivityUtils.startActivity(new Intent(activity, FrontAdActivity.class));
                    }
                }
            } catch (Exception e) {

            }

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            mFinalCount--;
            Log.e(TAG, "onActivityStopped: " + activity.getClass().getSimpleName() + ">>>" + mFinalCount);
            if (mFinalCount == 0) {
                //说明从前台回到了后台
                FrontAdConfig.saveLastFrontAdShowTime(System.currentTimeMillis());
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    };

    public void init() {
        Utils.getApp().registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
        //启动界面不显示广告页
        addToAdvFilter(SplashActivity.class);
        addToAdvFilter(FrontAdActivity.class);
    }

    private FrontADService() {

    }

    public static FrontADService getInstance() {
        if (instance == null) {
            synchronized (FrontADService.class) {
                if (instance == null) {
                    instance = new FrontADService();
                }
            }
        }
        return instance;
    }

    /**
     * Add activity to advertisement filter.
     *
     * @param clazz clazz
     */
    public void addToAdvFilter(Class<? extends Activity> clazz) {
        String key = clazz.getName();
        String activityName = clazz.getSimpleName();
        if (!advFilter.containsKey(key))
            advFilter.put(key, activityName);
    }

    /**
     * Remove activity from advertisement filter.
     *
     * @param clazz clazz
     */
    public void removeFromAdvFilter(Class<? extends Activity> clazz) {
        String key = clazz.getName();
        if (advFilter.containsKey(key))
            advFilter.remove(key);
    }

    private boolean canShowAdvertisement(Class<? extends Activity> clazz) {
        String key = clazz.getName();
        return !advFilter.containsKey(key) && key.startsWith(PKG_NAME);
    }
}
